﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using WebUI.Controls;
using MyGeneration.dOOdads;

namespace WebUI.EditPages
{
    public partial class CallCustomer : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            lstRepair.ClientOnCallCustomerPrototype = "callCustomer";
            lstRepair.AllowUpdating = false;

            if (!IsPostBack)
            {
                ReloadAll();


                Customers custs = Customers.GetAllActiveCustomers();
                Utilities.LoadDropDownListNoSort(lstCust, custs.DefaultView, Customers.ColumnNames.CustomerName, Customers.ColumnNames.ID, "--All--");
            }

            txtJobID.Focus();

        }

        private void ReloadAll()
        {
            View_RepairJobs jbs = View_RepairJobs.GetDoneOrRejectedJobs();
            jbs.Sort = View_RepairJobs.ColumnNames.CreatedDate + " desc";
            lstRepair.Jobs = jbs;
            lstRepair.SortExpression = View_RepairJobs.ColumnNames.CreatedDate;
            lstRepair.SortDirection = BaseListControl.SORT_DESC;
            lstRepair.Refresh();
            lstRepair.Columns[2].Visible = true;
        }

        protected void btnLoadAll_OnClick(object sender, EventArgs e)
        {
            ReloadAll();
        }

        protected void lstCust_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstCust.SelectedIndex > 0)
                lstRepair.Filter = "CustomerID " + "=" + lstCust.SelectedValue;
            else
                lstRepair.Filter = "";
            lstRepair.Refresh();
            lstRepair.Columns[2].Visible = true;
            lstRepair.Filter = "";
        }

        protected void btnLoadByJobID_OnClick(object sender, EventArgs e)
        {
            lstRepair.Filter = View_RepairJobs.ColumnNames.RepairNo + " LIKE '%" + txtJobID.Text + "%'";
            lstRepair.Refresh();
            lstRepair.Columns[2].Visible = true;
            lstRepair.Filter = "";
        }

        protected void btnCallCustomer_OnClick(object sender, EventArgs e)
        {
            

            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();

                lstRepair.Filter = View_RepairJobs.ColumnNames.ID + " = " + hidJobID.Value;
                lstRepair.SourceObject.MarkAsDeleted();

                int id = int.Parse(hidJobID.Value);
                RepairJob job = new RepairJob();
                job.LoadByPrimaryKey(id);

                job.StatusID = RepairStatus.CalledCustomer;
                job.LastModifiedByUser = SessionUser.ID;
                job.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);

                job.Save();

                RepairStatusHistories hist = new RepairStatusHistories();
                hist.AddNew();
                hist.RepairJobID = job.ID;
                hist.RepairStatusID = job.StatusID;
                hist.UpdatedByUser = SessionUser.ID;
                hist.UpdatedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                hist.Save();

                tx.CommitTransaction();

                lstRepair.Filter = "";
                lstRepair.Refresh();
                lstRepair.Columns[2].Visible = true;
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                lstRepair.Filter = "";
                ErrorLog.WriteToFile("UpdateStatus.aspx.cs", "btnSaveAndClose_OnClick", ex.StackTrace);
                ((Popup)Master).ShowErrorMessage("Error: " + ex.Message);
            }
        }

    }
}